package cn.zbh.pet.controller;


import cn.zbh.basic.annotation.PreAuthorize;
import cn.zbh.basic.util.AjaxResult;
import cn.zbh.basic.util.PageList;
import cn.zbh.org.domain.Department;
import cn.zbh.pet.domain.Pet;
import cn.zbh.pet.domain.PetType;
import cn.zbh.pet.query.PetQuery;
import cn.zbh.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/pet")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * http://localhost:8080/Pet  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有宠物信息" )
    public List<Pet> findAll(){

        return petService.findAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/Pet/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public Pet findById(@PathVariable("id") Long id){
        return petService.findById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            petService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/Pet
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public AjaxResult updateOrAdd(@RequestBody Pet Pet){

        try {
            if(Pet.getId()==null){
                // 添加操作
                petService.add(Pet);
            }else{
                // 修改操作
                petService.update(Pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }




    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){

        try {
            petService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，稍后重试");
        }
    }


    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        try {
            // 哈哈哈
            return petService.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Pet>();
        }
    }


    /**
     * 上架
     * @param
     * @return
     */
    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public AjaxResult onsale(@RequestBody List<Long> ids){

        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * 下架
     * @param
     * @return
     */
    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public AjaxResult offsale(@RequestBody List<Long> ids){

        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，稍后重试");
        }
    }
}


